'''
处理电阻相关的值，直接对数据进行处理
'''

from excutor.excutor import Excutor
import re

class Resistance(Excutor):

    def __init__(self,data):
        self.data = data
        self.resistance = ['DC电阻','电阻','电阻公差','电阻值(25℃)','谐振电阻','阻抗']
        # 筛选电阻数字的正则表达式，包含mkM，以及省略零的小数
        #[.]?[0-9]+[.]?[0-9]*[mkM]*
        self.patternNumber = re.compile('[.]?[0-9]+[.]?[0-9]*[mkMK]*')
        #筛选电阻公差的正则表达式
        #[±+-]?[0-9]+[.]?[0-9]*[%Ω]?
        self.patternDiffer = re.compile('[±+-]?[0-9]+[.]?[0-9]*[%Ω]?')


    #判断是否存在电阻数据
    @staticmethod
    def Existence(data,resistance = ['DC电阻','电阻','电阻公差','电阻值(25℃)','谐振电阻','阻抗']):
        return data[data['new_key'].isin(resistance)]['new_key'].count()

    #修改数据表
    def processingData(self):
        for new_key in self.resistance:
            # 筛选出new_key对应的old_value数据
            old_values = self.data[self.data['new_key'] == new_key]\
                        ['old_value'] 
            #找到上述行的index值，以便存储数据
            indexOld_value = self.data[self.data['new_key'] == new_key].index
            new_values = []
            if new_key != '电阻公差':
                #修改除电阻公差外的所有电阻相关值
                for old_value in old_values:
                    new_value = self.changeValue1(old_value)
                    new_values.append(new_value)
            else:
                #修改电阻公差的值
                for old_value in old_values:
                    new_value = self.changeValue2(old_value)
                    new_values.append(new_value)            
            #将修改后的值写入数据
            #用loc方法可以用index索引查找特定多行 
            self.data.loc[indexOld_value,'new_value_ref'] = new_values
        return self.data


    #修改数据(无电阻公差)
    def changeValue1(self,old_value):
        old_value = old_value.replace(' ','')
        #筛选数字的正则表达式，包含mkM，以及省略零的小数  self.patternNumber
        #选出的数字列表
        numbers = self.patternNumber.findall(old_value)
        numbersTemp = []
        # 循环将简写小数点变完整
        for num in numbers:
            numTemp = self.compeletDecimal(num) #补全小数
            numTemp = self.Ktok(numTemp)#将大写K改为小写k
            numTemp = self.dropZero(numTemp)#将小数点后无用的0去掉
            numbersTemp.append(numTemp)
        numbers = numbersTemp
        Ohms = 'Ω'
        #判断数字列表长度
        if len(numbers) == 1:
            #一个电阻值的返回
            return self.oneNumberReturn(numbers,old_value) + Ohms
        elif len(numbers) > 1:
            #多个电阻值的返回
            return self.mutipleNumberReturn(numbers,old_value,Ohms)
        else:
            #没有数字时，返回原值
            return old_value


    #修改数据(电阻公差)
    def changeValue2(self,old_value):
        #消除空格
        old_value = old_value.replace(' ','')
        #公差列表
        differs = self.patternDiffer.findall(old_value)
        #给需要的值增加%
        differsTemp = []
        for differ in differs:
            if '%' not in differ:
                if 'Ω' in differ:
                    differsTemp.append(differ)
                else:
                    differsTemp.append(differ + '%')
            else:
                differsTemp.append(differ)
        differs = differsTemp
        #判断长度
        if len(differs)<1:
            return old_value
        elif len(differs) == 1:
            return differs[0]
        else:
            return ','.join(differs)






    #将小数点后无用的0去掉
    def dropZero(self,num):
        patternDropZero = '(?=.)[.]?[0]+[mkM]?$'
        if '.' in num:
            nums = re.split(patternDropZero,num)
            if len(nums) >1:
                if 'm' in num :
                    return nums[0] + 'm'
                elif 'k' in num :
                    return nums[0] + 'k'
                elif 'M' in num:
                    return nums[0] + 'M'
                else:
                    return nums[0]
            else:
                return num
        else:
            return num



    #将简写小数变为正常小数 例如.11
    def compeletDecimal(self,num):
        if num[0] == '.':
            return '0' + num
        else:
            return num

    #将大写K改为小写k
    def Ktok(self,num):
        if 'K' in num:
            return num.replace('K','k')
        else:
            return num

    #一个电阻值的返回值,传入数字列表和value
    def oneNumberReturn(self,numbers,old_value):
        if 'meg' in old_value:
            return numbers[0].replace('m','M')
        else:
            return numbers[0]

    #处理存在多个电阻值时的返回
    def mutipleNumberReturn(self,numbers,old_value,Ohms):
        #先处理如R1=100ohms的数据
        if 'R' in old_value:
            if len(numbers) == 2:
                return numbers[1] + Ohms
            else:
                return numbers[1]+Ohms+','+numbers[3]+Ohms
        elif '-' in old_value or '~' in old_value or 'to' in old_value:
            return numbers[0]+Ohms+' to '+numbers[1]+Ohms
        elif len(numbers) == 3:
            return numbers[0]+Ohms+','+numbers[2]+Ohms
        else:
            return numbers[0]+Ohms+','+numbers[1]+Ohms
